﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using SilverlightDebateView;
using SilverFlow.Controls;
using System.Linq;
using SilverlightDebateViewModel;

namespace SilverlightDebateView.Behaviors
{    
    public class OpenStatementBehavior : TargetedTriggerAction<FloatingWindowHost>
    {
        FloatingWindowHost objFloatingWindowHost;
        private double xpos = 50;
        private double ypos = 50;
        private Point startPoint;

        #region OnAttached
        protected override void OnAttached()
        {
            base.OnAttached();
            objFloatingWindowHost = (FloatingWindowHost)(this.AssociatedObject);
        } 
        #endregion

        #region OnDetaching
        protected override void OnDetaching()
        {
            base.OnDetaching();
        } 
        #endregion

        #region Invoke
        protected override void Invoke(object parameter)
        {
            if (SelectedStatement != null)
            {
                // Only add if not already added
                var result = (from fWindow in objFloatingWindowHost.Children.Cast<FrameworkElement>()
                              where fWindow.Tag as string == SelectedStatement.StatementID.ToString()
                              select fWindow).FirstOrDefault();

                if (result == null)
                {
                    Statement StatementWindow = new Statement();

                    // Get DataContext of MainPageViewModel
                    MainPageViewModel objMainPageViewModel = (MainPageViewModel)objFloatingWindowHost.DataContext;

                    // Get DataContext of StatementViewModel
                    StatementViewModel objStatementViewModel = (StatementViewModel)StatementWindow.DataContext;

                    // Add MainPageViewModel to StatementViewModel
                    objStatementViewModel.VMMainPageViewModel = objMainPageViewModel;

                    // Add The StatementWindow 
                    objFloatingWindowHost.Add(StatementWindow);
                    string title = SelectedStatement.Title;
                    StatementWindow.Tag = SelectedStatement.StatementID.ToString();
                    StatementWindow.Title = title;
                    StatementWindow.IconText = title;

                    // Set WSindow Position
                    startPoint = new Point(xpos, ypos);
                    // Show Window
                    StatementWindow.Show(startPoint);

                    // Bring the window to the front
                    BringWindowToFront(StatementWindow);

                    // Set the Selected Statement
                    objStatementViewModel.SetSelectedStatemenet(SelectedStatement);

                    // increase xpos and ypos
                    xpos = xpos + 20;
                    ypos = ypos + 20;
                }
                else
                {
                    // Bring the window to the front
                    BringWindowToFront((result as Statement));        
                }
            }
        }
        #endregion

        // Utility

        #region BringWindowToFront
        private void BringWindowToFront(Statement StatementWindow)
        {
            int HighestZIndex = 0;
            foreach (FrameworkElement fWindow in objFloatingWindowHost.Children)
            {
                int zValue = ((int)fWindow.GetValue(Canvas.ZIndexProperty));
                if (zValue > HighestZIndex)
                {
                    HighestZIndex = zValue + 1;
                }

                fWindow.SetValue(Canvas.ZIndexProperty, zValue);
                objFloatingWindowHost.UpdateLayout();
            }

            StatementWindow.SetValue(Canvas.ZIndexProperty, HighestZIndex++);
        }
        #endregion

        // Dependancy Property

        public static readonly DependencyProperty SelectedStatementProperty =
        DependencyProperty.Register("SelectedStatement",
        typeof(SilverlightDebateViewModel.SilverlightDebateWS.Statement), typeof(OpenStatementBehavior), null);

        public SilverlightDebateViewModel.SilverlightDebateWS.Statement SelectedStatement
        {
            get
            {
                return (SilverlightDebateViewModel.SilverlightDebateWS.Statement)base.GetValue(SelectedStatementProperty);
            }
            set
            {
                base.SetValue(SelectedStatementProperty, value);
            }
        }

    }
}
